<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
		<button @click="getUserInfo" type="primary" class="btn-login">一键登录</button>
		<text>登录后尽享更多权利</text>
		
	</view>
</template>
<script>
	import {
		mapMutations,mapState
	} from 'vuex'
	export default {
		computed:{
				...mapState('user',['token','redirectInfo'])
		},
		data() {
			return {

			};
		},
		methods: {
			...mapMutations('user', ['updateUserInfo','updatetoken','updateRedirectInfo']),
			getUserInfo() {
				const that = this
				uni.getUserProfile({
					desc: '用于完善会员资料', // 必填，声明用途
					success(res) {
						that.updateUserInfo(res.userInfo)
						that.getToken(res)
						console.log('用户信息', res.userInfo);
					},
					fail(err) {
						console.log('用户拒绝或基础库过低', err);
					}
				});
			},
			navigateBack(){
				if(this.redirectInfo && this.redirectInfo.openType === 'switchTab'){
					const that = this
					uni.switchTab({
						url:this.redirectInfo.from,
						success() {
							that.updateRedirectInfo(null)
						}
					})
				}
			},
			async getToken(info) {
				console.log(info)
				const res = await uni.login().catch(err => err)
				if (res.errMsg !== 'login:ok') return uni.$showMsg('登录失败')
				const query = {
					code: res.code,
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				const {data:httpRes} = await uni.$http.get('http://localhost:8080/wx/user/login',query)
				console.log(httpRes)
				if(httpRes.statuCode !==200 ) {
					return  uni.$showMsg('登录失败')
				}
				uni.$showMsg('登录成功')
				this.updatetoken(httpRes.result.token)
				this.navigateBack()
				console.log(res)
			}
		}
	}
</script>
<style lang="scss">
	.login-container {
		height: 750rpx;
		background-color: #f8f8f8;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: relative;

		&::after {
			content: ' ';
			display: block;
			width: 100%;
			height: 40px;
			background-color: white;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%);
		}

		.btn-login {
			width: 90%;
			border-radius: 100px;
			margin: 15px 0;
			background-color: red;
		}
	}
</style>